package cn.com.cis.web.actions;

import java.util.Collection;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.cis.domain.JobInfo;
import cn.com.cis.domain.JobLog;
import cn.com.cis.job.ETLServer;
import cn.com.cis.service.JobLogService;

@Controller
@RequestMapping(value = "/queue")
public class ExecutorController {
	
	@Autowired
    private JobLogService logService;
    
    @Autowired
    private ETLServer etlServer;

    private void setNavigateBar(ModelAndView mv){
    	mv.addObject("icon", "icon-list");
        mv.addObject("title", "等待队列");
    }

    @RequiresPermissions("配置管理:等待队列:查看等待队列列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView("queue/list");
        setNavigateBar(mav);
        
        mav.addObject("queue", etlServer.getBlockingDeque());
        
        return mav;
    }

    @RequiresPermissions("配置管理:等待队列:查看等待队列列表")
    @RequestMapping(value = "/MainAjaxlist", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public  Collection<JobInfo> MainAjaxlist() {
        return etlServer.getBlockingDeque();
    }

    @RequiresPermissions("配置管理:等待队列:查看等待队列列表")
    @RequestMapping(value = "/ajaxList", method = RequestMethod.GET)
    @ResponseBody
    public Collection<JobLog> ajaxList() {
        return logService.selectAllJobLog(1,10);
    }

    /*
    @Autowired
    private JobEntityService jobEntityService;
    
    @Autowired
    private ParameterService parameterService;
    
    @Autowired
    private ExecutorService executorService;
    
    @Autowired
    private LayerService layerService;
    
    // TODO
    @RequestMapping(value = "/executeAll", method = RequestMethod.GET)
    public ModelAndView executeAll(HttpServletRequest request, RedirectAttributesModelMap modelMap) throws StandardException, ParseException {
        ModelAndView mav = new ModelAndView("redirect:/main");
        setNavigateBar(mav);
        
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        
        if (user == null) {
            mav.setViewName("redirect:/login");
            return mav;
        }
        
        List<Layer> layerList = layerService.selectAllLayer();
        for (Layer layer : layerList) {
            List<JobEntity> jobList = jobEntityService.selectJobEntityByLayer(layer.getId(), true);
            for (JobEntity job : jobList) {
                JobInfo jobInfo = new JobInfo();
                jobInfo.setJobId(job.getId());
                
                if(etlServer.getBlockingDeque().contains(jobInfo)){
                    modelMap.addFlashAttribute("error","不能重复提交作业。");
                    mav.setViewName("redirect:/layer/list");
                    return mav;
                }
                
                Set<String> params = new HashSet<String>();
                SqlScriptUtil.getSqlParameter(params, job.getSqlScript());
                List<Parameter> parameterList = parameterService.selectParameterByJobId(job.getId());
                if (params.size() != parameterList.size()) {
                    modelMap.addFlashAttribute("error", "作业参数与sql定义不否!");
                    return mav;
                }
                
                for (Parameter parameter : parameterList) {
                    if (Constants.GLOBAL.equals(parameter.getScope())) {
                        if ("".equals(parameter.getParameterRef().getDefaultVal()) || parameter.getParameterRef().getDefaultVal().length() <= 0) {
                            modelMap.addFlashAttribute("error", "全局参数必须赋初始值!");
                            return mav;
                        }
                    } else {
                        if (parameter.getParameterValue() == null) {
                            modelMap.addFlashAttribute("error", "本地参数必须赋初始值!");
                            return mav;
                        }
                    }
                }
            }
        }
        
        executorService.execute(user);
        
        return mav;
    }


    // TODO
    @RequestMapping(value = "/start/{layerId}", method = RequestMethod.GET)
    public ModelAndView start(@PathVariable(value = "layerId") int layerId, RedirectAttributesModelMap modelMap, HttpServletRequest request) throws ParseException, StandardException {
        ModelAndView mav = new ModelAndView("redirect:/layer/list");
        setNavigateBar(mav);
        
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            mav.setViewName("redirect:/login");
            return mav;
        }
        
        List<JobEntity> jobList = jobEntityService.selectJobEntityByLayer(layerId, true);
        for (JobEntity job : jobList) {
            Set<String> params = new HashSet<String>();
            SqlScriptUtil.getSqlParameter(params, job.getSqlScript());
            List<Parameter> parameterList = parameterService.selectParameterByJobId(job.getId());
            
            JobInfo jobInfo = new JobInfo();
            jobInfo.setJobId(job.getId());
            
            if(etlServer.getBlockingDeque().contains(jobInfo)){
                modelMap.addFlashAttribute("error","不能重复提交作业。");
                mav.setViewName("redirect:/layer/list");
                return mav;
            }
            
            if (params.size() != parameterList.size()) {
                modelMap.addFlashAttribute("error", "作业参数与sql定义不否!");
                mav.setViewName("redirect:/layer/list");
                return mav;
            }
            
            for (Parameter parameter : parameterList) {
                if (Constants.GLOBAL.equals(parameter.getScope())) {
                    if ("".equals(parameter.getParameterRef().getDefaultVal()) || parameter.getParameterRef().getDefaultVal().length() <= 0) {
                        modelMap.addFlashAttribute("error", "全局参数必须赋初始值!");
                        mav.setViewName("redirect:/layer/list");
                        return mav;
                    }
                } else {
                    if (parameter.getParameterValue() == null) {
                        modelMap.addFlashAttribute("error", "本地参数必须赋初始值!");
                        mav.setViewName("redirect:/layer/list");
                        return mav;
                    }
                }
            }
        }
        
        Layer layer = layerService.selectLayerById(layerId);
        executorService.execute(layer, user);
        
        mav.setViewName("redirect:/layer/list");
        
        return mav;
    }

    // TODO
    @RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView add(int jobId, RedirectAttributesModelMap modelMap, HttpServletRequest request,
			@RequestParam(value = "page", required = true, defaultValue = "1") int page) throws ParseException, StandardException {
		ModelAndView mav = new ModelAndView("redirect:/job/list?page=" + page);
		setNavigateBar(mav);

		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			mav.setViewName("redirect:/login");
			return mav;
		}
		
		JobInfo jobInfo = new JobInfo();
		jobInfo.setJobId(jobId);
		if (etlServer.getBlockingDeque().contains(jobInfo)) {
			modelMap.addFlashAttribute("error", "不能重复提交作业。");
			mav.setViewName("redirect:/job/list?page=" + page);
			return mav;
		}
		
		if (checkJobParameter(jobId)) {
			modelMap.addFlashAttribute("error", "作业参数校验失败，请检查作业参数");
			mav.setViewName("redirect:/job/list?page=" + page);
			return mav;
		}
		
		JobEntity jobEntity = jobEntityService.selectJobEntityById(jobId);
		executorService.execute(jobEntity, user);
		
		return mav;
	}

    // TODO
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public ModelAndView delete(HttpServletRequest request, int jobId) {
        ModelAndView mav = new ModelAndView("redirect:/queue/list");
        setNavigateBar(mav);
        
        BlockingDeque<JobInfo> deque = etlServer.getBlockingDeque();
        
        JobInfo jobInfo = new JobInfo();
        jobInfo.setJobId(jobId);
        deque.remove(jobInfo);
        
        return mav;
    }

    // TODO
    @RequestMapping(value = "/stop", method = RequestMethod.GET)
    public ModelAndView stop() throws InterruptedException {
        ModelAndView mav = new ModelAndView("redirect:/main");
        setNavigateBar(mav);
        
        etlServer.shutdownNow();
        
        return mav;
    }

     // TODO
    @RequestMapping(value = "/start", method = RequestMethod.GET)
    public ModelAndView startEtlService() {
        ModelAndView mav = new ModelAndView("redirect:/main");
        setNavigateBar(mav);
        
        etlServer.start();
        
        return mav;
    }

    private boolean checkJobParameter(int jobId) {
        JobEntity jobEntity = jobEntityService.selectJobEntityById(jobId);
        List<Parameter> jobParameterList = parameterService.selectParameterByJobId(jobId);
        
        Set<String> params = new HashSet<String>();
        SqlScriptUtil.getSqlParameter(params, jobEntity.getSqlScript());
        if (params.size() != jobParameterList.size()) {
            return true;
        }
        
        for (Parameter parameter : jobParameterList) {
            if (Constants.GLOBAL.equals(parameter.getScope())) {
                if ("".equals(parameter.getParameterRef().getDefaultVal()) || parameter.getParameterRef().getDefaultVal().length() <= 0) {
                    return true;
                }
            } else {
                if (parameter.getParameterValue() == null) {
                    return true;
                }
            }
        }
        
        return false;
    }
    */

}
